<template>
  <div class="dispatchInfo">
    <editForm
      :formConfig="formConfig"
      ref="basicForm"
      :formData="formData"
    ></editForm>
  </div>
</template>

<script>
export default {
  props: {
    dispatchInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    unEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formConfig: {
        labelWidth: "140px",
        itemArr: [
          {
            label: "是否外单位",
            prop: "isOut",
            type: "select",
            required: true,
            disabled: false,
            span: 8,
            isHide: false,
            selectOption: [
              { label: "是", value: 1 },
              { label: "否", value: 0 },
            ],
            methods: (val) => {
              this.isOutOrgChange(val.value);
            },
          },
          {
            label: "工作跟踪负责人",
            prop: "trackLeaderName", //多选时，字段要为数组类型
            idsStr: "trackLeader",
            multiple: false, //是否多选
            isOutOrg: 0, //是否是外单位;0:不是外单位；1是外单位；不做限制就不传此参数
            type: "user", //类型
            trigger: "change",
            isHide: true,
            methods: (obj, formData) => {
              //this.$refs.basicForm.setFormItem("trackLeader", obj.acount);
            },
            required: true,
            disabled: false,
            span: 8,
          },
          {
            label: "工作负责人",
            prop: "leaderName",
            idsStr: "leader",
            propArr: "leaderArr",
            orgNameStr: "deptWorkName",
            orgIdStr: "deptWork",
            isOutOrg: 0, //是否是外单位;0:不是外单位；1是外单位；不做限制就不传此参数
            allowCreate: false, //是否允许创建条目
            option: [], //this.deptOption1
            type: "user-select", //类型
            trigger: "change",
            isHide: false,
            multiple: false,
            methods: (data, formData) => {
              this.handleOperateOrg();
            },
            required: true,
            disabled: false,
            span: 8,
          },
          {
            label: "工作班成员",
            prop: "classUser",
            propArr: "classUserArr",
            idsStr: "classUserId",
            orgStr: "classOrgStr",
            orgNameStr: "classOrgName",
            orgIdStr: "classOrgId",
            multiple: true, //是否多选
            allowCreate: true, //是否允许创建条目
            option: [], //this.deptOption1
            type: "user-select", //类型
            trigger: "change",
            isHide: false,
            methods: (data, formData) => {
              this.handleOperateOrg();
              let arr = data.map((item) => {
                let obj = {
                  worker: item.id,
                  workerName: item.label,
                  jobDept: item.orgId,
                  jobDeptName: item.orgName,
                  staffType: item.isOutOrg,
                };
                return obj;
              });
              this.$refs.basicForm.setFormItem("workers", arr);
            },
            required: false,
            disabled: false,
            span: 8,
            rows: 2,
          },
          {
            label: "作业单位",
            prop: "workerDeptName",
            propArr: "workerDeptArr",
            idsStr: "workerDeptId",
            //level: "3",
            multiple: true,
            allowCreate: true,
            option: [], //this.deptOption
            type: "dept-select", //类型
            trigger: "change",
            methods: (data, formData) => {
              let arr = data.map((item) => {
                let obj = {
                  workDept: item.value,
                  workDeptName: item.label,
                };
                return obj;
              });
              this.$refs.basicForm.setFormItem("workerDept", arr);
            },
            span: 8,
            rows: 2,
          },
          {
            label: "注意事项",
            prop: "attention",
            type: "textarea",
            isHide: false,
            required: false,
            disabled: false,
            span: 8,
            rows: 3,
          },
        ],
      },
      formData: {
        isOut: 0,
      },
      userOption: [],
      orgArr: [],
    };
  },
  created() {
    if (this.unEdit) {
      this.formConfig.itemArr.map((i) => (i.disabled = true));
    } else {
      this.formConfig.itemArr.map((i) => {
        if (i.prop != "orgList") {
          i.disabled = false;
        }
      });
    }
    this.init(this.dispatchInfo);
  },
  mounted() {
    //this.handleOperateOrg();
  },
  watch: {
    dispatchInfo: {
      handler(val) {
        this.init(val);
      },
    },
    unEdit: {
      handler(val) {
        if (val) {
          this.formConfig.itemArr.map((i) => (i.disabled = true));
        } else {
          this.formConfig.itemArr.map((i) => {
            if (i.prop != "orgList") {
              i.disabled = false;
            }
          });
        }
      },
    },
  },
  methods: {
    init(val) {
      this.formData = Object.assign({}, this.formData, val);
      this.$set(this.formData, "classOrgArr", []);
      let isOut = val ? val.isOut : 0;
      this.isOutOrgChange(isOut);
      const classInfo = {
        classUser: [],
        classUserArr: [],
        classUserId: [],
        classOrgName: [],
        classOrgId: [],
        classOrgStr: [],
      };
      let leaderOption = [
        {
          label: this.formData.leaderName,
          value: this.formData.leader,
          id: this.formData.leader,
          isOutOrg:this.formData.leaderName==this.formData.leader?-1:0,
          orgName: this.formData.deptWorkName,
          orgId: this.formData.deptWork,
        },
      ];
      setTimeout(() => {
        this.$refs.basicForm.setItemOption("leaderName", leaderOption);
        this.$refs.basicForm.setFormItem("leaderArr", this.formData.leader);
      }, 0);

      if (this.formData.workers) {
        this.formData.workers.forEach((item) => {
          classInfo.classUser.push(item.workerName);
          classInfo.classOrgStr.push(item.staffType);
          if (item.jobDept) {
            classInfo.classOrgName.push(item.jobDeptName);
            classInfo.classOrgId.push(item.jobDept);
            classInfo.classUserId.push(item.worker);
            classInfo.classUserArr.push(`${item.worker}${item.jobDept}`);
          } else {
            classInfo.classUserId.push(item.worker);
            classInfo.classOrgName.push("");
            classInfo.classOrgId.push("");
            classInfo.classUserArr.push(item.workerName);
          }
          return;
        });
        this.formData.classUser = classInfo.classUser.join(",");
        this.formData.classUserId = classInfo.classUserId.join(",");
        this.formData.classOrgName = classInfo.classOrgName.join(",");
        this.formData.classOrgId = classInfo.classOrgId.join(",");
        this.formData.classOrgStr = classInfo.classOrgStr.join(",");
        this.formData.classUserArr = classInfo.classUserArr;
        this.userOption = this.getOptionData(
          classInfo.classUser,
          classInfo.classUserId,
          classInfo.classOrgStr,
          classInfo.classOrgName,
          classInfo.classOrgId
        );

        setTimeout(() => {
          this.$refs.basicForm.setItemOption("classUser", this.userOption);
          this.$refs.basicForm.setFormItem(
            "classUserArr",
            classInfo.classUserArr
          );
        }, 0);
      }
      if (this.formData.workerDept) {
        let arr = this.formData.workerDept;
        arr.map((item) => {
          item.label = item.workDeptName;
          item.value = item.workDept;
          item.id = item.workDept;
          item.name = item.workDeptName;
        });
        this.formData.workerDeptName = arr
          .map((item) => item.workDeptName)
          .join(",");
        this.formData.workerDeptId = arr.map((item) => item.workDept).join(",");
        let workerDeptArr = arr.map((item) => item.workDept);
        setTimeout(() => {
          this.$refs.basicForm.setItemOption("workerDeptName", arr);
          this.$refs.basicForm.setFormItem("workerDeptArr", workerDeptArr);
        }, 0);
      }
    },
    getOptionData(nameArr, idArr, orgArr, orgName, orgId) {
      let option = [];
      nameArr.map((i, index) => {
        let item = {
          label: i,
          value: `${idArr[index]}${orgId[index]}`,
          id: idArr[index],
          isOutOrg: orgArr[index],
          orgName: orgName[index],
          orgId: orgId[index],
        };
        option.push(item);
      });
      return option;
    },
    //作业单位回显
    orgListEcho() {
      let arr = this.isRepeat(this.orgArr);
      let orgStr = arr.map((item) => item.orgName).join(",");
      this.formData.orgList = orgStr;
    },
    //是否外单位改变
    isOutOrgChange(val) {
      if (val == 1) {
        this.formConfig.itemArr[2].isOutOrg = 1;
        this.formConfig.itemArr[2].allowCreate = true;
        this.formConfig.itemArr[1].isHide = false;
      } else {
        this.formConfig.itemArr[1].isHide = true;
        this.formConfig.itemArr[2].isOutOrg = 0;
        this.formConfig.itemArr[2].allowCreate = false;
      }
    },
    //处理作业单位
    handleOperateOrg() {
      this.orgArr = [];
      let formData = this.$refs.basicForm.getCurrentData();
      if (formData.deptWorkName) {
        this.orgArr.push({
          orgId: formData.deptWork,
          orgName: formData.deptWorkName,
        });
      }
      if (formData.classOrgName) {
        formData.classOrgName.split(",").forEach((item, index) => {
          if (item != "") {
            let obj = {
              orgId: formData.classOrgId.split(",")[index],
              orgName: item,
            };
            this.orgArr.push(obj);
          }
        });
      }
      let arr = this.isRepeat(this.orgArr);
      arr.forEach((item) => {
        item.label = item.orgName;
        item.value = item.orgId;
        item.id = item.orgId;
        item.name = item.orgName;
      });
      let workerDeptName = arr.map((item) => item.orgName).join(",");
      let workerDeptArr = arr.map((item) => item.orgId);
      let workerDeptId = arr.map((item) => item.orgId).join(",");
      this.$refs.basicForm.setFormItem("workerDeptName", workerDeptName);
      this.$refs.basicForm.setFormItem("workerDeptId", workerDeptId);
      this.$refs.basicForm.setItemOption("workerDeptName", arr);
      this.setWorkerDept(arr);
      setTimeout(() => {
        this.$refs.basicForm.setFormItem("workerDeptArr", workerDeptArr);
      }, 0);
    },
    //设置作业单位
    setWorkerDept(data) {
      let arr = data.map((item) => {
        let obj = {
          workDept: item.value,
          workDeptName: item.label,
        };
        return obj;
      });
      this.$refs.basicForm.setFormItem("workerDept", arr);
    },
    //数组去重
    isRepeat(data) {
      let result = [];
      let obj = {};
      for (var i = 0; i < data.length; i++) {
        if (!obj[data[i].orgId]) {
          result.push(data[i]);
          obj[data[i].orgId] = true;
        }
      }
      return result;
    },
    //获取派单信息
    async getDispatchInfo() {
      try {
        let formData = await this.$refs.basicForm.getFormData();
        return formData;
        //delete formData.
      } catch (error) {
        this.$message.error(error);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.dispatchInfo {
  width: 100%;
  height: 100%;
}
</style>